package com.tutu.service;

import com.tutu.core.Constant;
import com.tutu.core.NoticeMsgType;
import com.tutu.entity.vo.BaseMsgVo;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.UnicodeUtil;
import com.tutu.utils.WxMsgPushUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQReceiver {

    @Autowired
    CodeDictService codeDictService;

    @Autowired
    DeviceService deviceService;

    @Autowired
    UserService userService;

    @Autowired
    CompanyService companyService;

    @Autowired
    EmissionCodeService emissionCodeService;

    @Autowired
    RoleService roleService;

    /**
     * 监听 msgExchange 的消息
     */
    // 监听所有路由键
    @RabbitListener(bindings = @org.springframework.amqp.rabbit.annotation.QueueBinding(value = @org.springframework.amqp.rabbit.annotation.Queue(value = "webAppDataQueue", durable = "true"), exchange = @org.springframework.amqp.rabbit.annotation.Exchange(value = "msgExchange", type = "topic"), key = "#"))
    public void receiveFromDataExchange(Object object) {
        String result = object + "";
        result = result.substring(0, result.indexOf("MessageProperties") - 2);
        result = result.substring(7);

        BaseMsgVo bmv = BaseUtil.getRabbitmqMsg(result);

        switch (bmv.getMsgType()) {
            case NoticeMsgType.SEND_WECHAT_MESSAGE:
                WxMsgPushUtil.receiveMsg(UnicodeUtil.unicodeDecode(bmv.getValueList().get(0)), UnicodeUtil.unicodeDecode(bmv.getValueList().get(1)), UnicodeUtil.unicodeDecode(bmv.getValueList().get(2)), Long.parseLong(bmv.getValueList().get(3)), bmv.getValueList().get(4));
                break;
            case NoticeMsgType.DELETE_CODE_DICT:
            case NoticeMsgType.UPDATE_CODE_DICT:
                codeDictService.loadCacheData();
                break;
            case NoticeMsgType.DELETE_USER:
            case NoticeMsgType.UPDATE_USER:
                userService.loadCacheData();
                break;
            case NoticeMsgType.DELETE_COMPANY:
            case NoticeMsgType.UPDATE_COMPANY:
                companyService.loadCacheData();
                break;
            case NoticeMsgType.DELETE_DEVICE:
            case NoticeMsgType.UPDATE_DEVICE:
                deviceService.loadCacheData(null);
                break;
            case NoticeMsgType.DELETE_EMISSION_CODE:
            case NoticeMsgType.UPDATE_EMISSION_CODE:
                emissionCodeService.loadCacheData();
                break;
            case NoticeMsgType.DELETE_ROLE:
            case NoticeMsgType.UPDATE_ROLE:
                roleService.loadCacheData();
                break;
            case NoticeMsgType.UPDATE_DEVICE_ONLINE_UP:
                deviceService.noticeDeviceStatusInfo(UnicodeUtil.unicodeDecode(bmv.getValueList().get(0)), bmv.getValueList().get(1), Constant.CHECK_STATUS_SUCCESS);
                break;
            case NoticeMsgType.UPDATE_DEVICE_ONLINE_DOWN:
                deviceService.noticeDeviceStatusInfo(UnicodeUtil.unicodeDecode(bmv.getValueList().get(0)), bmv.getValueList().get(1), Constant.CHECK_STATUS_DEFAULT);
                break;
        }
    }
}